package com.edu.controller;

import com.edu.dao.ProductCategoryMapper;
import com.edu.data.ProductCategory;
import com.edu.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@Controller
@RequestMapping("/productCategory")
public class ProductCategoryController {
    SqlSession sqlSession= MyBatisUtil.createSession();
    ProductCategoryMapper productCategoryMapper=sqlSession.getMapper(ProductCategoryMapper.class);

    @RequestMapping("/toProductCategory")
    private void toProductCategory(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
        List<ProductCategory> productCategories = productCategoryMapper.getCategories(null);
        req.setAttribute("categories",productCategories);
        req.getRequestDispatcher("../static/manage/productClass.jsp").forward(req,resp);
    }

    @RequestMapping("/readProductCategory")
    private void readProductCategory(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
        ProductCategory productCategory = productCategoryMapper.findcategoryById(Long.parseLong(req.getParameter("entityId")));
        req.setAttribute("entityId", productCategory.getEpcId());
        req.setAttribute("name", productCategory.getEpcName());
        req.setAttribute("parentId",productCategory.getEpcParentId().equals(productCategory.getEpcId()) ? 0 : productCategory.getEpcParentId());
        List<ProductCategory> categories = productCategoryMapper.selectRootdirectory(null);
        req.setAttribute("categories", categories);
        req.getRequestDispatcher("../static/manage/productClass-modify.jsp").forward(req,resp);
    }

    @RequestMapping("/delProductCategory")
    private void delProductCategory(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        productCategoryMapper.deleteCategoryByid(Integer.parseInt(req.getParameter("entityId")));
        resp.sendRedirect("toProductCategory");
    }

    @RequestMapping("/saveProductCategory")
    private void saveProductCategory(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException {
        if (req.getParameter("entityId").trim().isEmpty()){
            ProductCategory category = new ProductCategory();
            if (!req.getParameter("parentId").equals("0")){
                category.setEpcParentId(Integer.parseInt(req.getParameter("parentId")));
            }
            category.setEpcName(req.getParameter("name"));
            productCategoryMapper.saveCategory(category);
        }else {
            Long parentId = Long.parseLong(req.getParameter("parentId"));
            List<ProductCategory> categoryList = productCategoryMapper.selectRootdirectory(Long.parseLong(req.getParameter("entityId")));
            if(parentId != 0){
                categoryList.get(0).setEpcParentId(Integer.parseInt(req.getParameter("parentId")));
            }
            categoryList.get(0).setEpcName(req.getParameter("name"));
            productCategoryMapper.updateCategory(categoryList.get(0));
        }
        resp.sendRedirect("toProductCategory");
    }

    @RequestMapping("/create")
    private void create(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
        List<ProductCategory> categoryList = productCategoryMapper.selectRootdirectory(null);
        req.setAttribute("categories",categoryList);
        req.setAttribute("parentId",0);
        req.getRequestDispatcher("../static/manage/productClass-modify.jsp").forward(req,resp);
    }
}
